package com.tencent.wxcloudrun.schedule;

import java.util.*;

import javax.annotation.Resource;

import com.tencent.wxcloudrun.constant.RedisKeyConstant;
import com.tencent.wxcloudrun.domain.TActi;
import com.tencent.wxcloudrun.domain.TActiJoin;
import com.tencent.wxcloudrun.enums.ActiJoinKindEnum;
import com.tencent.wxcloudrun.enums.ActiJoinStatEnum;
import com.tencent.wxcloudrun.enums.ActiJoinTypeEnum;
import com.tencent.wxcloudrun.service.TActiJoinService;
import com.tencent.wxcloudrun.util.NumberUtils;
import com.tencent.wxcloudrun.util.RedisUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.tencent.wxcloudrun.manager.ActiManager;
import com.tencent.wxcloudrun.service.TActiService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

/**
 * @author Ryan Zhao
 *         <p>
 *         Copyright: Copyright (c) 2023
 *         </p>
 * @version 1.0
 * @date 2023-08-22 12:21
 * @description: 活动入围结束任务
 */
@Slf4j
@Component
public class ActiSelectedEndJob implements Job {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private ActiManager actiManager;

    @Resource
    private TActiService actiService;

    @Resource
    private TActiJoinService actiJoinService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void execute(JobExecutionContext jobExecutionContext) {
        String actiIdStr = Objects.toString(jobExecutionContext.getJobDetail().getJobDataMap().get("param"));
        Long actiId = Long.valueOf(actiIdStr);
        log.info("活动入围结束定时任务-处理开始-活动序号=[{}]", actiId);

        // 活动入围结束
        actiService.actiSelectedEnd(actiId);

        // 更新缓存
        actiManager.saveRedis4Acti(actiId, false);

        TActi acti = actiService.getById(actiId);
        if (Objects.isNull(acti)) {
            return;
        }
        // 如果是摇号
        if (acti.getSelectedJoinType() == ActiJoinTypeEnum.SHAKE.getValue()) {
            // 生成中选号码
            List<Integer> numberList = getRandomNumber(acti);
            // 更新参选状态
            List<TActiJoin> actiJoinList = actiJoinService.actiSelectedEnd(actiId, numberList);
            // 更新缓存
            if (!CollectionUtils.isEmpty(actiJoinList)) {
                Map<String, String> redisMap = new HashMap<>();
                for (TActiJoin actiJoin : actiJoinList) {
                    String key = RedisKeyConstant.getActiJoinStatusRedisKey(actiId, actiJoin.getUserId());
                    redisMap.put(key, String.valueOf(ActiJoinStatEnum.ALREADY_SELECTED.getValue()));
                }
                redisUtil.multiSet(redisMap);
            }
        }

        log.info("活动入围结束定时任务-处理成功");
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/5/7
     * @Params: [actiId]
     * @Return: List<Integer>
     * @Description: 获取入围号码
     */
    private List<Integer> getRandomNumber(TActi acti) {
        String selectMaxNumber = redisUtil.get(RedisKeyConstant.getActiSelectedNumberRedisKey(acti.getId()));
        return NumberUtils.getRandomNumber(1, Integer.parseInt(selectMaxNumber) + 1, acti.getSelectedCount());
    }

}
